﻿Public Class frmTaxDE
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmTaxDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        validator.ResetControls(grpTax)
        PopulateTaxCode(cboTaxCode)

        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                'Dim taxList As List(Of Tax) = sqlList.getItemTax(Clipboard.GetData(DataFormats.Text))
                Dim taxList As List(Of Tax) = payrollManager.getItemTax(Clipboard.GetData(DataFormats.Text))
                For Each tax As Tax In taxList
                    Dim taxCodeList As List(Of TaxCode) = payrollManager.getItemTaxCodeByID(tax.tax_code_id)
                    For Each taxCode As TaxCode In taxCodeList
                        cboTaxCode.Text = taxCode.tax_code_desc
                    Next
                    txtMinimumSalary.Text = tax.min_salary
                    txtMaximumSalary.Text = tax.max_salary
                    txtBaseTax.Text = tax.base_tax
                    txtPercentageOver.Text = tax.percentage_over
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If


    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsNumericString(txtMinimumSalary, "Minimum Salary") Then Return
        If validator.IsNumericString(txtMaximumSalary, "Maximum Salary") Then Return
        If validator.IsNumericString(txtBaseTax, "Monthly Salary") Then Return
        If validator.IsNumericString(txtPercentageOver, "Employee Share") Then Return

        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                payrollManager.insertTax(cboTaxCode.SelectedValue.ToString(), txtMinimumSalary.Text, txtMaximumSalary.Text, txtBaseTax.Text, txtPercentageOver.Text)
                'sqlManager.executeQuery("INSERT INTO tbl_tax (tax_code_id,min_salary,max_salary,base_tax,percentage_over) VALUE (" + cboTaxCode.SelectedValue.ToString() + "," + txtMinimumSalary.Text + "," + txtMaximumSalary.Text + "," + txtBaseTax.Text + "," + txtPercentageOver.Text + ")")
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemTax(cboTaxCode.SelectedValue.ToString(), txtMinimumSalary.Text, txtMaximumSalary.Text, txtBaseTax.Text, txtPercentageOver.Text, Clipboard.GetData(DataFormats.Text))
                'SQLManager.executeQuery("UPDATE tbl_tax SET tax_code_id=" + cboTaxCode.SelectedValue.ToString() + " , min_salary=" + txtMinimumSalary.Text + ", max_salary=" + txtMaximumSalary.Text + ", base_tax=" + txtBaseTax.Text + ", percentage_over=" + txtPercentageOver.Text + " WHERE id=" + Clipboard.GetData(DataFormats.Text) + " ")
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()

    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        validator.ResetControls(grpTax)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpTax)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
    Public Function PopulateTaxCode(ByVal cbo As ComboBox)

        Dim dataTable As New DataTable
        dataTable.Columns.Add("id")
        dataTable.Columns.Add("tax_code_desc")
        'Dim taxcodeList As List(Of TaxCode) = sqlList.getAllTaxCode()
        Dim taxcodeList As List(Of TaxCode) = payrollManager.getAllTaxCode()
        For Each taxcode As TaxCode In taxcodeList
            dataTable.Rows.Add(taxcode.id, taxcode.tax_code_desc)
        Next

        cbo.DataSource = dataTable
        cbo.DisplayMember = "tax_code_desc"
        cbo.ValueMember = "id"
        cbo.SelectedIndex = -1

        Return (0)
    End Function
End Class